#include "sylar/head.h"
#include "sylar/socket.h"

static sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

void test()
{
	sylar::IPAddress::ptr addr = sylar::Address::LookupAnyIPAddress("www.baidu.com");
	if(addr){
		SYLAR_LOG_INFO(g_logger) << "get address=" <<addr->toString();
	} else{
		SYLAR_LOG_ERROR(g_logger) << " failed ";
		return ;
	}

	sylar::Socket::ptr sock = sylar::Socket::CreateTCP(addr);
	addr->setPort(80);
	if(!sock->connect(addr)){
		SYLAR_LOG_ERROR(g_logger) << "connect " << addr->toString() << "failed";
		return ;
	} else {
		SYLAR_LOG_INFO(g_logger) << "connect " << addr->toString() << " connected";
	}

	const char buff[] = "GET / HTTP/1.0\r\n\r\n";
	int rt = sock->send(buff, sizeof(buff));
	
	if(rt <= 0) {
		SYLAR_LOG_ERROR(g_logger) << "send failed";
		return ;
	}

	std::string buffs;
	buffs.resize(4096);
	rt = sock->recv(&buffs[0], buffs.size());

	if(rt <= 0){
		SYLAR_LOG_ERROR(g_logger) << "recv failed";
		return ;
	}
	buffs.resize(rt);
	SYLAR_LOG_INFO(g_logger) << buffs;
	
}

int main(int argc, char **argv){
	sylar::IOManager iom(1);
	iom.schedule(test);
	return 0;
}